package com.leetcode;

public class StringReverse
{
	public static void main(String[] args)
	{
		StringReverse sr = new StringReverse();
		System.out.println(sr.swapWords("I LOVE United States"));
		
  	}
	
	public String swapWords(String str)
	{
		char[] arr = str.toCharArray();
		
		swap(arr, 0, arr.length-1);
		int begin = 0;
		for (int i = 1; i < arr.length; i++)
		{
			if(arr[i] == ' ')
			{
				swap(arr, begin, i-1);
				begin = i + 1;
			}
		}
		return new String(arr);
	}
	
	public void swap(char[] arr, int begin, int end)
	{
		char tmp;
		while(begin < end)
		{
			tmp = arr[begin];
			arr[begin] = arr[end];
			arr[end] = tmp;
			
			begin++;
			end--;
		}
	}
	
	
}
